package com.xuyiwei.xblog.modular.article.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.xuyiwei.xblog.business.system.model.Dict;
import com.xuyiwei.xblog.core.base.constant.enums.CommonStatusEnum;
import com.xuyiwei.xblog.core.base.controller.BaseController;
import com.xuyiwei.xblog.core.base.response.Result;
import com.xuyiwei.xblog.core.common.constant.factory.ConstantFactory;
import com.xuyiwei.xblog.core.exception.BizExceptionEnum;
import com.xuyiwei.xblog.core.exception.GunsException;
import com.xuyiwei.xblog.core.node.ZTreeNode;
import com.xuyiwei.xblog.core.util.ToolUtil;
import com.xuyiwei.xblog.business.article.model.Type;
import com.xuyiwei.xblog.business.article.service.ITypeService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.xuyiwei.xblog.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;

/**
 * 文章类型表控制器
 *
 * @author fengshuonan
 * @Date 2018-10-15 20:57:06
 */
@Controller
@RequestMapping("/article/type")
public class TypeController extends BaseController {

    private String PREFIX = "/article/type/";

    @Autowired
    private ITypeService typeService;

    /**
     * 跳转到文章类型首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "type.html";
    }

    /**
     * 跳转到添加文章类型
     */
    @RequestMapping("/type_add")
    public String typeAdd() {
        return PREFIX + "type_add.html";
    }

    /**
     * 跳转到修改文章类型
     */
    @RequestMapping("/type_update/{typeId}")
    public String typeUpdate(@PathVariable Integer typeId, Model model) {
        Type type = typeService.selectById(typeId);
        model.addAttribute("item",type);
        model.addAttribute("pName", ConstantFactory.me().getArticleTypeName(type.getPid()));
        LogObjectHolder.me().set(type);
        return PREFIX + "type_edit.html";
    }

    /**
     * 获取文章类型列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Result<List<Type>> list(String condition) {
        Result<List<Type>> result = Result.instance();
        try{
            Wrapper<Type> wrapper = new EntityWrapper<>();
            wrapper.eq("status", CommonStatusEnum.USE.getKey());
            List<Type> bizTypes = typeService.selectList(wrapper);
            result.setData(bizTypes);
        }catch (Exception e){
            result.setError("500","查询文章类型错误");
        }
        return result;
    }

    /**
     * 新增文章类型
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Result<Boolean> add(Type type) {
        Result<Boolean> result = Result.instance();
        try {
            type.setUpdateTime(new Date());
            type.setCreateTime(new Date());
            type.setStatus(CommonStatusEnum.USE.getKey());
            typeService.insert(type);
            result.setData(true);
        }catch (Exception e){
            result.setError("500","新增文章类型出错");
        }
        return result;
    }

    /**
     * 删除文章类型
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Result<Boolean> delete(@RequestParam(required = true, value = "ids[]") List<Long> ids) {
        Result<Boolean> result = Result.instance();
        try{
            if (ToolUtil.isEmpty(ids)) {
                throw new GunsException(BizExceptionEnum.REQUEST_NULL);
            }
            this.typeService.deleteTypeByIds(ids);
            result.setData(true);
        }catch (Exception e){
            result.setError("500","删除文章类型出错");
        }
        return result;
    }

    /**
     * 修改文章类型
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Result<Boolean> update(Type type) {
        Result<Boolean> result = Result.instance();
        try {
            type.setUpdateTime(new Date());
            typeService.updateById(type);
            result.setData(true);
        }catch (Exception e){
            result.setError("500","修改文章类型出错");
        }
        return result;
    }

    /**
     * 文章类型详情
     */
    @RequestMapping(value = "/detail/{typeId}")
    @ResponseBody
    public Result<Type> detail(@PathVariable("typeId") Integer typeId) {
        Result<Type> result = Result.instance();
        try {
            Type type = typeService.selectById(typeId);
            result.setData(type);
        }catch (Exception e){
            result.setError("500","获取文章类型详情出错");
        }
        return  result;
    }

    /**
     * 文章类型树形类型
     */
    @RequestMapping(value = "/tree")
    @ResponseBody
    public Result<List<ZTreeNode>> tree() {
        Result<List<ZTreeNode>> result = Result.instance();
        try{
            List<ZTreeNode> tree = this.typeService.tree();
            tree.add(ZTreeNode.createParent());
            result.setData(tree);
        }catch (Exception e){
            result.setError("500","获取文章类型树形类型出错");
        }
        return result;
    }
}
